package com.srwl.mytx.widget;


import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.coordinatorlayout.widget.CoordinatorLayout;

import com.google.android.material.bottomsheet.BottomSheetBehavior;

public class BottomSheetBehaviorV1<V extends View> extends BottomSheetBehavior {
    private static final String TAG = "BottomSheetBehaviorV1";
    private int downX;
    private int downY;

    public BottomSheetBehaviorV1() {
    }

    public BottomSheetBehaviorV1(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(CoordinatorLayout parent, View child, MotionEvent event) {

        int action = event.getActionMasked();


        switch (action) {
            case MotionEvent.ACTION_DOWN:
                // Log.d(TAG, "onInterceptTouchEvent:ACTION_DOWN 执行了，，，");
                downX = (int) event.getX();
                downY = (int) event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                //  Log.d(TAG, "onInterceptTouchEvent:滑动了，，，滑动了，，，，getState() " + getState());
                int initialX = (int) event.getX();
                int initialY = (int) event.getY();
                //点击是在底部弹出视图内 且 x 方向滑动距离小于y 方向滑动距离  且  是向下滑动了5个像素  ，开始拦截 滑动事件
                if (parent.isPointInChildBounds(child, downX, downY) && Math.abs(initialX - downX) < Math.abs(initialY - downY) && initialY - downY > 5) {
                    Log.e(TAG, "onInterceptTouchEvent: 开始拦截滑动事件，，");
                    return true;
                }
                break;
        }

        return super.onInterceptTouchEvent(parent, child, event);

    }


    public static <V extends View> BottomSheetBehaviorV1<V> fromv1(V view) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (!(params instanceof androidx.coordinatorlayout.widget.CoordinatorLayout.LayoutParams)) {
            throw new IllegalArgumentException("The view is not a child of CoordinatorLayout");
        } else {
            CoordinatorLayout.Behavior behavior = ((androidx.coordinatorlayout.widget.CoordinatorLayout.LayoutParams) params).getBehavior();
            if (!(behavior instanceof BottomSheetBehaviorV1)) {
                throw new IllegalArgumentException("The view is not associated with BottomSheetBehavior");
            } else {
                return (BottomSheetBehaviorV1) behavior;
            }
        }
    }

}
